﻿using CqCore.UploadService.Base;
using System;
using System.IO;
using UploadService.Extensions;
using UploadService.Option;

namespace UploadService.Base
{
    /// <summary>
    /// 固定尺寸的Uploader
    /// </summary>
    public abstract class FileUploaderBase : IFileUploader
    {
        private readonly ImageServiceOption option;
        public FileUploaderBase(ImageServiceOption option)
        {
            this.option = option;
            FolderYearMonth = DateTime.Now.ToString("yyyyMMdd");
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="stream"></param>
        /// <param name="reason"></param>
        /// <returns></returns>
        public abstract bool Validate(string fileName, Stream stream, out string reason);

        public abstract int MaxBytesLength { get; }
        /// <summary>
        /// 文件类型
        /// </summary>
        public abstract string[] AllowedImageTypes { get; }
        /// <summary>
        /// 文件名称
        /// </summary>
        public abstract string FolderName { get; }

        /// <summary>
        /// 按月份存文件目录
        /// </summary>
        public virtual string FolderYearMonth { get; set; }

        /// <summary>
        /// 临时文件相对路径
        /// </summary>
        public string TempRelativePath
        {
            get { return Path.Combine("/", Path.GetFileName(option.FileUploadPath), Path.GetFileName(this.TempPhysicalPath)).ToForwardSlashPath(); }
        }

        private string tempPhysicalPath;
        /// <summary>
        /// 临时文件物理路径
        /// </summary>
        public virtual string TempPhysicalPath
        {
            get
            {
                if (tempPhysicalPath == null)
                {
                    tempPhysicalPath = Path.Combine(option.FileUploadPath, option.FileUploadFolderNameTemp);
                    if (!System.IO.Directory.Exists(tempPhysicalPath))
                    {
                        System.IO.Directory.CreateDirectory(tempPhysicalPath);
                    }
                }
                return tempPhysicalPath;
            }
        }



        private string physicalPath;
        /// <summary>
        /// 文件物理路径
        /// </summary>
        public string PhysicalPath
        {
            get
            {
                if (physicalPath == null || !System.IO.Directory.Exists(physicalPath))
                {
                    var physicaps = Path.Combine(option.FileUploadPath, this.FolderName);
                    if (!System.IO.Directory.Exists(physicaps))
                    {
                        System.IO.Directory.CreateDirectory(physicaps);
                    }
                    physicaps = Path.Combine(physicaps, this.FolderYearMonth);
                    if (!System.IO.Directory.Exists(physicaps))
                    {
                        System.IO.Directory.CreateDirectory(physicaps);
                    }

                    physicalPath = physicaps;
                }
                return physicalPath;
            }
        }


        /// <summary>
        /// 文件相对路径
        /// </summary>
        public string RelativePath
        {
            get { return Path.Combine("/", Path.GetFileName(option.FileUploadPath), this.FolderName, this.FolderYearMonth).ToForwardSlashPath(); }
        }


    }
}
